package com.itboye.lottery.activity;

import android.content.Intent;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.handmark.pulltorefresh.library.ObservableScrollView;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshScrollView;
import com.itboye.lottery.base.BaseActivity;
import com.itboye.lottery.R;
import com.itboye.lottery.adapter.FindChildAdapter;
import com.itboye.lottery.alert.ByAlert;
import com.itboye.lottery.bean.FindBean;
import com.itboye.lottery.presenter.UserPresenter;
import com.itboye.lottery.retrofit.ResponseCallback;
import com.itboye.lottery.utils.ConstUrl;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;

import static com.itboye.lottery.utils.ConstUrl.ACTIVITY_DETAIL;

public class PlayMethodActivity extends BaseActivity implements ResponseCallback {

    RecyclerView rv;
    UserPresenter userPresenter = new UserPresenter(this);
    private int position;
    private ArrayList<FindBean> findBeanArrayList = new ArrayList<>();
    PullToRefreshScrollView pull_to_refresh_scrollView;
    private FindChildAdapter findChildAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_play_method;
    }

    int page = 1;

    @Override
    protected void initData() {
        setTitle(getIntent().getStringExtra("title"));
        position = getIntent().getIntExtra("position", 0);
        findChildAdapter = new FindChildAdapter(this, findBeanArrayList);
        findChildAdapter.setTypePosition(position);
        rv.setLayoutManager(new LinearLayoutManager(this));
        rv.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
        rv.setNestedScrollingEnabled(false);
        rv.setAdapter(findChildAdapter);
        findChildAdapter.setOnItemClickListener((view, position) -> {
            if (PlayMethodActivity.this.position == 0) {
                startActivity(new Intent(PlayMethodActivity.this, WebActivity.class)
                        .putExtra("title", findBeanArrayList.get(position).getName())
                        .putExtra("url", ConstUrl.play_method_url + "?type=" + findBeanArrayList.get(position).getId()));
            } else if (PlayMethodActivity.this.position == 2) {
                startActivity(new Intent(PlayMethodActivity.this, WebActivity.class)
                        .putExtra("title", "活动详情")
                        .putExtra("url", ACTIVITY_DETAIL + findBeanArrayList.get(position).getArticle_id()));
            } else {
                startActivity(new Intent(PlayMethodActivity.this, FindDetailActivity.class)
                        .putExtra("position", PlayMethodActivity.this.position)
                        .putExtra("title", getIntent().getStringExtra("title"))
                        .putExtra("article_id", findBeanArrayList.get(position).getArticle_id())
                );
            }
        });
        getData();
        initScrollViewListener();
    }

    private void initScrollViewListener() {
        if (position != 0) {
            pull_to_refresh_scrollView.setMode(PullToRefreshBase.Mode.BOTH);
            pull_to_refresh_scrollView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ObservableScrollView>() {
                @Override
                public void onPullDownToRefresh(PullToRefreshBase<ObservableScrollView> refreshView) {
                    page = 1;
                    getData();
                }

                @Override
                public void onPullUpToRefresh(PullToRefreshBase<ObservableScrollView> refreshView) {
                    page++;
                    getData();
                }
            });
        }
    }

    void getData() {
        switch (position) {
            case 0:
                userPresenter.getPlayCenter();
                break;
            case 1:
                userPresenter.getNews(page);
                break;
            case 2:
                userPresenter.getAtivities(page);
                break;
            case 3:
                userPresenter.getNewsNotice(page);
                break;
        }
    }

    @Override
    public void onClick(View view) {

    }

    @Override
    public void success(@NotNull Object o, @NotNull String requestSuccessType) {
        pull_to_refresh_scrollView.onRefreshComplete();
        if (requestSuccessType == userPresenter.getFind_SUCCESS) {
            ArrayList<FindBean> findBeanArrayList = (ArrayList<FindBean>) o;
            if (page == 1) {
                this.findBeanArrayList.clear();
            }
            this.findBeanArrayList.addAll(findBeanArrayList);
            findChildAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void fail(int code, @NotNull Object msg, @NotNull String requestErrorType) {
        pull_to_refresh_scrollView.onRefreshComplete();
        if (requestErrorType == userPresenter.getFind_FAIL) {
            ByAlert.alert(msg);
        }
    }
}
